<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Walter Tamboer
 * @package Framework03
 */

class FW_Database_Table
{
    /**
     * @var FW_Database_Adapter_Abstract The database adapter used to executes query.
     */
    private static $adapter;

    /**
     * Sets the database adapter.
     * @param FW_Database_Adapter_Abstract $adapter
     */
    public static function setAdapter(FW_Database_Adapter_Abstract $adapter)
    {
        self::$adapter = $adapter;
    }
    
    /**
     * Gets the database adapter.
     * @return FW_Database_Adapter_Abstract
     */
    public static function getAdapter()
    {
        return self::$adapter;
    }

    /**
     * Returns the name of the table used to build queries.
     * @return string The name of the database table.
     */
    public function getTableName()
    {
        return get_class($this);
    }

    /**
     * Creates a select statement that executes on this database table.
     * @param array $columns The columns that should be selected.
     * @return FW_Database_Statement_Select
     */
    public function createSelectStatement(array $columns)
    {
        return new FW_Database_Statement_Select($this, $columns);
    }

    /**
     * Creates an insert statement that executes on this database table.
     * @param array $data The data that should be inserted.
     * @return FW_Database_Statement_Insert
     */
    public function createInsertStatement(array $data)
    {
        return new FW_Database_Statement_Insert($this, $data);
    }

    /**
     * Creates an update statement that executes on this database table.
     * @param array $data The data that should be updated.
     * @return FW_Database_Statement_Update
     */
    public function createUpdateStatement(array $data)
    {
        return new FW_Database_Statement_Update($this, $data);
    }

    /**
     * Creates a truncate statement that executes on this database table.
     * @return FW_Database_Statement_Truncate
     */
    public function createTruncateStatement()
    {
        return new FW_Database_Statement_Truncate($this);
    }

    /**
     * Creates a drop table statement that executes on this database table.
     * @return FW_Database_Statement_DropTable
     */
    public function createDropStatement()
    {
        return new FW_Database_Statement_DropTable($this);
    }

    /**
     * Select the given columns from this database table.
     * @param array $columns The columns that should be selected.
     * @return FW_Database_Adapter_Rowset
     */
    public function select(array $columns)
    {
        $statement = $this->createSelectStatement($columns);
        
        return $this->getAdapter()->execute($statement);
    }

    /**
     * Inserts the given data into this table.
     * @param array $data The data that should be inserted.
     * @return FW_Database_Table
     */
    public function insert(array $data)
    {
        $statement = $this->createInsertStatement($data);
        
        $this->getAdapter()->execute($statement);

        return $this;
    }

    /**
     * Truncates this table.
     * @return FW_Database_Table
     */
    public function truncate()
    {
        $statement = $this->createTruncateStatement();
        
        $this->getAdapter()->execute($statement);
        
        return $this;
    }

    /**
     * Drops this table.
     * @return FW_Database_Table
     */
    public function drop()
    {
        $statement = $this->createDropStatement();
        
        $this->getAdapter()->execute($statement);
        
        return $this;
    }
}
